package com.molihub.utils;

import com.alibaba.fastjson.JSONObject;
import com.molihub.entity.Visitor;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDateTime;

/**
 * @ClassName: AddressUtils
 * @Description: TODO
 * @author: xw
 * @date 2020/2/16 13:18
 * @Version: 1.0
 **/
@Component
public class AddressUtils {
    public static Visitor getVisitor(String ip) {
        if ("127.0.0.1".equals(ip)|| "0:0:0:0:0:0:0:1".equals(ip)){
            return Visitor.builder()
                    .ip(ip).time(LocalDateTime.now()).country("内网IP")
                    .city("内网IP").region("beijing").isp("内网IP").build();
        }
        String responseMsg = "";
        String getURL = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;
        URL getUrl = null;
        try {
            getUrl = new URL(getURL);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            System.out.println("获取URl对象失败!");
        }
        HttpURLConnection connection = null;
        try {
            connection = (HttpURLConnection) getUrl.openConnection();
            connection.connect();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer sb = new StringBuffer();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            // 断开连接
            connection.disconnect();
            responseMsg = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("连接服务器失败!");
        }
        if (responseMsg != null) {
            JSONObject address = JSONObject.parseObject(responseMsg);
            JSONObject jsonObject = new JSONObject(address);
            jsonObject = jsonObject.getJSONObject("data");
            //通过相应的get方法,获取相应的属性
            //国家
            String county = jsonObject.getString("country_id");
            //省份
            String region = jsonObject.getString("region");
            //城市
            String city = jsonObject.getString("city");
            //运营商
            String isp = jsonObject.getString("isp");
            return Visitor.builder()
                    .ip(ip).time(LocalDateTime.now()).country(county)
                    .city(city).region(convertRegion(region)).isp(isp).build();
        }
        return null;
    }

    private static String convertRegion(String region) {
        if ("河北".equals(region)) {
            return "hebei";
        } else if ("江苏".equals(region)) {
            return "jiangsu";
        } else if ("河南".equals(region)) {
            return "henan";
        } else if ("安徽".equals(region)) {
            return "anhui";
        } else if ("浙江".equals(region)) {
            return "zhejiang";
        } else if ("辽宁".equals(region)) {
            return "liaoning";
        } else if ("北京".equals(region)) {
            return "beijing";
        } else if ("吉林".equals(region)) {
            return "jilin";
        } else if ("上海".equals(region)) {
            return "shanghai";
        } else if ("广西".equals(region)) {
            return "guangxi";
        } else if ("四川".equals(region)) {
            return "sichuan";
        } else if ("贵州".equals(region)) {
            return "guizhou";
        } else if ("湖南".equals(region)) {
            return "hunan";
        } else if ("山东".equals(region)) {
            return "shandong";
        } else if ("广东".equals(region)) {
            return "guangdong";
        } else if ("江西".equals(region)) {
            return "jiangxi";
        } else if ("福建".equals(region)) {
            return "fujian";
        } else if ("云南".equals(region)) {
            return "yunnan";
        } else if ("江苏".equals(region)) {
            return "jiangsu";
        } else if ("江苏".equals(region)) {
            return "jiangsu";
        } else if ("江苏".equals(region)) {
            return "jiangsu";
        } else if ("海南".equals(region)) {
            return "hainan";
        } else if ("山西".equals(region)) {
            return "shanxi";
        } else if ("内蒙古".equals(region)) {
            return "neimongol";
        } else if ("江苏".equals(region)) {
            return "jiangsu";
        } else if ("天津".equals(region)) {
            return "tianjin";
        } else if ("甘肃".equals(region)) {
            return "gansu";
        } else if ("陕西".equals(region)) {
            return "shaanxi";
        } else if ("澳门".equals(region)) {
            return "macau";
        } else if ("香港".equals(region)) {
            return "hongkong";
        } else if ("台湾".equals(region)) {
            return "taiwan";
        } else if ("青海".equals(region)) {
            return "qinghai";
        } else if ("西藏".equals(region)) {
            return "xizang";
        } else if ("宁夏".equals(region)) {
            return "ningxia";
        } else if ("新疆".equals(region)) {
            return "xinjiang";
        } else if ("黑龙江".equals(region)) {
            return "heilongjiang";
        } else if ("重庆".equals(region)) {
            return "chongqing";
        } else {
            return "未知省份";
        }
    }
}
